package com.dj.mingkong.common.infrastructure.base.dto.page;

import java.io.Serializable;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @author zenglonghui
 * @date: 2022/5/16 18:24
 */
public interface IPage<T> extends Serializable {
    /**
     * @deprecated
     */
    @Deprecated
    default Map<Object, Object> condition() {
        return null;
    }

    default boolean optimizeCountSql() {
        return true;
    }

    default boolean isSearchCount() {
        return true;
    }

    default long offset() {
        long current = this.getCurrent();
        return current <= 1L ? 0L : (current - 1L) * this.getSize();
    }

    default Long maxLimit() {
        return null;
    }

    default long getPages() {
        if (this.getSize() == 0L) {
            return 0L;
        } else {
            long pages = this.getTotal() / this.getSize();
            if (this.getTotal() % this.getSize() != 0L) {
                ++pages;
            }

            return pages;
        }
    }

    default IPage<T> setPages(long pages) {
        return this;
    }

    /**
     * @deprecated
     */
    @Deprecated
    default void hitCount(boolean hit) {
    }

    /**
     * @deprecated
     */
    @Deprecated
    default boolean isHitCount() {
        return false;
    }

    List<T> getRecords();

    IPage<T> setRecords(List<T> var1);

    long getTotal();

    IPage<T> setTotal(long var1);

    long getSize();

    IPage<T> setSize(long var1);

    long getCurrent();

    IPage<T> setCurrent(long var1);

    default <R> IPage<T> convert(Function<? super T, ? extends R> mapper) {
        List<T> collect = (List<T>) this.getRecords().stream().map(mapper).collect(Collectors.toList());
        return this.setRecords(collect);
    }

    default String countId() {
        return null;
    }
}
